package pl.uz.zgora.plan;

import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;

import org.ksoap2.SoapEnvelope;
import org.ksoap2.SoapFault;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.HttpTransportSE;
import org.xmlpull.v1.XmlPullParserException;

import android.app.Activity;
import android.content.Context;
import android.widget.ArrayAdapter;
import android.widget.ListView;

public class KalendarzSOAPManager {
	private SoapObject soapRequest = null;
	private SoapSerializationEnvelope soapEnvelope = null;
	private HttpTransportSE httpSOAPTransport = null;
	
	public KalendarzSOAPManager() {
		soapEnvelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
		httpSOAPTransport = new HttpTransportSE(SOAPManager.SERVICE_URL);
	}
	
	/** 
	 * Wywołuje wybrane metody webservice'u
	 * @throws XmlPullParserException 
	 * @throws IOException 
	 */
	private void callSOAPMethod(final String SOAP_ACTION) throws IOException, XmlPullParserException {
			httpSOAPTransport.call(SOAP_ACTION, soapEnvelope);
	}
	
	/** 
	 * Pobiera liste kalendarzy
	 * @throws XmlPullParserException 
	 * @throws IOException 
	 */
	public void downloadCalendarsList(final Context context) throws IOException, XmlPullParserException {
		/** Nazwa funkcji do wywołania */
		final String METHOD_NAME = "pobierzListeKalendarzy";
		final String SOAP_ACTION = SOAPManager.SERVICE_NAMESPACE + "#" + METHOD_NAME;

		soapRequest = new SoapObject(SOAPManager.SERVICE_NAMESPACE, METHOD_NAME);
		soapEnvelope.setOutputSoapObject(soapRequest);

		callSOAPMethod(SOAP_ACTION);

		SoapObject soapResponse = (SoapObject) soapEnvelope.getResponse();
		// Ilość pobranych obiektów
		final Integer responseObjectsCount = soapResponse.getPropertyCount();

		ArrayList<KalendarzTyp> calendarsList = new ArrayList<KalendarzTyp>(responseObjectsCount);

		for(int index = 0; index < responseObjectsCount; index++) {
			final Object property = soapResponse.getProperty(index);
			if(property instanceof SoapObject) {

				final KalendarzTyp kaledarzInfo = new KalendarzTyp((SoapObject)property);

				calendarsList.add(kaledarzInfo);
			}
		}

		PlanUzDataSource ds = new PlanUzDataSource(context);
		ds.writeCalendarsInfoList(calendarsList);
	}
	
	/** 
	 * Pobiera wybrany kalendarz
	 * @return 
	 * @throws ParseException 
	 * @throws XmlPullParserException 
	 * @throws IOException 
	 */
	public ArrayList<Dzien> downloadCalendar(String calendarId) throws ParseException, IOException, XmlPullParserException {
		/** Nazwa funkcji do wywołania */
		final String METHOD_NAME = "pobierzKalendarz";
		final String SOAP_ACTION = SOAPManager.SERVICE_NAMESPACE + "#" + METHOD_NAME;

		soapRequest = new SoapObject(SOAPManager.SERVICE_NAMESPACE, METHOD_NAME);
		soapRequest.addProperty("id", calendarId);
		soapEnvelope.setOutputSoapObject(soapRequest);

		callSOAPMethod(SOAP_ACTION);

		SoapObject soapResponse = (SoapObject) soapEnvelope.getResponse();
		// Ilość pobranych obiektów
		final Integer responseObjectsCount = soapResponse.getPropertyCount();

		ArrayList<Dzien> calendar = new ArrayList<Dzien>();
		Calendar cal = Calendar.getInstance();
		final SimpleDateFormat parser = new SimpleDateFormat("dd-MM-yyyy");
		Date calendarDate = null;
		int month = cal.get(Calendar.MONTH);
		int day = cal.get(Calendar.DAY_OF_MONTH);
		for(int index = 0; index < responseObjectsCount; index++) {
			final Object property = soapResponse.getProperty(index);
			if(property instanceof SoapObject) {

				final Dzien dzien = new Dzien((SoapObject)property);

				calendarDate = parser.parse(dzien.getData());
				if(calendarDate.getMonth() == month) {
					if(calendarDate.getDate() >= day) {
						calendar.add(dzien);
					}
				} else if(calendarDate.getMonth() > month) {
					calendar.add(dzien);
				}
			}
		}
		
		return calendar;
	}
}
